package com.mrd.modules.system.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrd.base.utils.DateUtils;
import com.mrd.base.utils.FileUtils;
import com.mrd.base.utils.PageUtils;
import com.mrd.base.utils.Query;
import com.mrd.base.utils.R;
import com.mrd.base.utils.ShiroUtils;
import com.mrd.base.utils.StringUtils;
import com.mrd.modules.attachment.AttachmentService;
import com.mrd.modules.system.entity.SysAttachmentEntity;
import com.mrd.modules.system.service.SysAttachmentService;

/**
 * 附件表
 * 
 * @author han
 * @date 2017-08-17 16:41:33
 */
@RestController
@RequestMapping("sysattachment")
public class SysAttachmentController {
  @Autowired
  private SysAttachmentService sysAttachmentService;
  @Autowired
  private AttachmentService attachmentService;

  /**
   * 列表
   */
  @RequestMapping("/list")
  public R list(@ModelAttribute SysAttachmentEntity sysAttachment,
      @RequestParam Map<String, Object> params) {
    // 查询列表数据
    Query query = new Query(params);
    PageHelper.startPage(query.getPage(), query.getLimit());
    List<SysAttachmentEntity> sysAttachmentList =
        sysAttachmentService.queryListByEntity(sysAttachment);
    PageInfo<SysAttachmentEntity> pi = new PageInfo<SysAttachmentEntity>(sysAttachmentList);
    PageUtils pageUtil = new PageUtils(sysAttachmentList, Integer.parseInt(pi.getTotal() + ""),
        query.getLimit(), query.getPage());
    return R.ok().put("page", pageUtil);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{id}")
  public R info(@PathVariable("id") Integer id) {
    SysAttachmentEntity sysAttachment = sysAttachmentService.queryObject(id);
    return R.ok().put("sysAttachment", sysAttachment);
  }

  /**
   * 保存
   */
  @ResponseBody
  @RequestMapping("/upload")
  public R upload(@RequestParam(value = "fileupload") MultipartFile fileUpload,
      HttpServletRequest request, HttpServletResponse response) {
    try {
      String originalFilename = fileUpload.getOriginalFilename();
      // String fileName = FileUtils.getFileName(originalFilename);
      String fileType = FileUtils.getFileType(originalFilename);
      String uuid = StringUtils.randomUUID();
      String otherName = uuid;
      String path = attachmentService.upload(fileUpload, otherName);
      SysAttachmentEntity sysAttachment = new SysAttachmentEntity();
      sysAttachment.setFileType(fileType);
      sysAttachment.setFileNo(StringUtils.randomUUID());
      sysAttachment.setFileName(originalFilename);
      sysAttachment.setFileNewName(otherName + "." + fileType);
      sysAttachment.setFileSize(fileUpload.getSize() + "");
      sysAttachment.setCdp(ShiroUtils.getUserEntity().getUsername());
      sysAttachment.setCdt(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
      sysAttachmentService.save(sysAttachment);
      return R.ok();
    } catch (IOException e) {
      return R.error(e.getMessage());
    }
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  public R update(@RequestBody SysAttachmentEntity sysAttachment) {
    sysAttachmentService.update(sysAttachment);
    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  public R delete(@RequestBody Integer[] ids) {
    List<SysAttachmentEntity> attachs = sysAttachmentService.queryByIds(ids);
    for (SysAttachmentEntity attachment : attachs) {
      attachmentService.delete(attachment.getFileNewName());
    }
    sysAttachmentService.deleteBatch(ids);
    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/down/{id}")
  public void down(@PathVariable("id") Integer id, HttpServletResponse response) {
    SysAttachmentEntity attachment = sysAttachmentService.queryObject(id);
    attachmentService.download(attachment.getFileNewName(), attachment.getFileName(), response);
  }
}
